package com.fractal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.fractal.R;

public class ColorChooser extends ListActivity 
{
	private final String TEXT_KEY = "text";
	private final String IMG_KEY = "img";
	private ListView listView;

	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);

		// adds listener to list view
		listView = getListView();
		listView.setOnItemClickListener(new OnItemClickListener() 
		{
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
			{
				setResult(arg2 + 1);
				finish();
			}
		});

		List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();

		generateData(resourceNames);

		SimpleAdapter adapter = new SimpleAdapter(this, 
												  resourceNames,
												  R.layout.listrow, 
												  new String[] { TEXT_KEY, IMG_KEY },
												  new int[] { R.id.text01, R.id.img01 });

		setListAdapter(adapter);
		listView = getListView();
	}


	private void generateData(List<Map<String, Object>> resourceNames) 
	{
		Map<String, Object> data;
		
		data = new HashMap<String, Object>();
		data.put(TEXT_KEY, "Random");
		data.put(IMG_KEY, R.drawable.random);
		resourceNames.add(data);
		
		data = new HashMap<String, Object>();
		data.put(TEXT_KEY, "Fire");
		data.put(IMG_KEY, R.drawable.fire);
		resourceNames.add(data);
		
		data = new HashMap<String, Object>();
		data.put(TEXT_KEY, "Sky");
		data.put(IMG_KEY, R.drawable.sky);
		resourceNames.add(data);
		
		data = new HashMap<String, Object>();
		data.put(TEXT_KEY, "Rainbow");
		data.put(IMG_KEY, R.drawable.rainbow);
		resourceNames.add(data);
		
		data = new HashMap<String, Object>();
		data.put(TEXT_KEY, "Rapid");
		data.put(IMG_KEY, R.drawable.rapid);
		resourceNames.add(data);
		
		data = new HashMap<String, Object>();
		data.put(TEXT_KEY, "Black and White");
		data.put(IMG_KEY, R.drawable.blackandwhite);
		resourceNames.add(data);
	}
}